package com.teachoa.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jfinal.plugin.activerecord.Page;
import com.teachoa.bean.Ask;
import com.teachoa.bean.AskReply;
import com.teachoa.bean.User;
import com.teachoa.enumtype.AskStatusEnum;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.IDUtil;

public class AskController extends BaseController {
	
	public void index() {
		int pageNo = getParaToInt("pageNo", 1);
		int userType = super.getCurrentUserType();
		String userId = super.getCurrentUserId();
		Page<Ask> askPage = Ask.dao.findAsksByPage(pageNo, userType, userId);
		if(userType!=UserTypeEnum.STUDENT.getCode()) {
			for(Ask ask:askPage.getList()) {
				String askId = ask.getStr("id");
				AskReply ar = AskReply.dao.findAskReplyByAidAndRid(askId, userId);
				Date replyTime = ar.getDate("reply_time");
				ask.put("can_reply", replyTime==null?1:0);
			}
		}
		setAttr("askPage", askPage);
		renderJsp("ask.jsp");
	}
	
	public void add() {
		String id = getPara();
		Ask ask = null;
		String studentId = super.getCurrentUserId();
		if(StringUtils.isNotEmpty(id)) {
			ask = Ask.dao.findById(id);
			setAttr("ask", ask);
			//回答过问题的人
			List<AskReply> replyList = AskReply.dao.getReplysByAskId(id);
			List<String> idList = new ArrayList<String>();
			for(AskReply askReply:replyList) {
				idList.add(askReply.getStr("replier_id"));
			}
			setAttr("userIds", StringUtils.join(idList.toArray(), ","));
			
			studentId = ask.getStr("student_id");
		}
		String oper = getPara("oper");
		
		//提问的专家和老师
		setAttr("experts", User.dao.findUserByType(UserTypeEnum.EXPERT.getCode()));
		setAttr("teachers", User.dao.getTeachersByStudentId(studentId));
		
		if("view".equals(oper)) {
			int status = ask.getInt("status");
			int isRead = ask.getInt("is_read");
			if(status==1&&isRead==0) {
				ask.set("is_read", 1);
				ask.update();
			}
			setAttr("replys", AskReply.dao.getFinishReplysByAskId(id));
			renderJsp("ask_view.jsp");
		} else {
			renderJsp("ask_add.jsp");
		}
			
	}
	
	public void save() {
		Ask ask = getModel(Ask.class);
		
		String id = ask.getStr("id");
		try {
			if(StringUtils.isEmpty(id)) {
				//存提问实体
				id = IDUtil.getId();
				ask.set("id", id);
				ask.set("status", AskStatusEnum.WAIT.getCode());
				ask.set("is_read", 0);
				ask.set("student_id", super.getCurrentUserId());
				ask.set("student_name", super.getCurrentUserName());
				ask.set("create_time", new Date());
				ask.save();
			} else {
				ask.update();
				//先删除关联
				AskReply.dao.deleteRelationsByAskId(id);
			}
			//存提问与回复者关系实体
			String[] userIds = getParaValues("userIds");
			for(String userId:userIds) {
				AskReply askReply = new AskReply();
				askReply.set("id", IDUtil.getId());
				askReply.set("ask_id", id);
				askReply.set("replier_id", userId);
				askReply.set("replier_name", User.dao.findById(userId).getStr("username"));
				askReply.save();
			}
			renderJson(success("保存成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("保存失败！！"));
		}
	}
	
	public void reply() {
		String id = getPara();
		Ask ask = Ask.dao.findById(id);
		setAttr("ask", ask);
		//回答过问题的人
		//setAttr("replys", AskReply.dao.getReplysByAskId(id));
		List<AskReply> replyList = AskReply.dao.getReplysByAskId(id);
		List<String> idList = new ArrayList<String>();
		List<AskReply> replys = new ArrayList<AskReply>();
		for(AskReply askReply:replyList) {
			Date replyTime = askReply.getDate("reply_time");
			if(replyTime!=null) 
				replys.add(askReply);
			idList.add(askReply.getStr("replier_id"));
		}
		setAttr("experts", User.dao.findUserByType(UserTypeEnum.EXPERT.getCode()));
		setAttr("teachers", User.dao.getTeachersByStudentId(ask.getStr("student_id")));
		setAttr("userIds", StringUtils.join(idList.toArray(), ","));
		setAttr("replys", replys);
		renderJsp("ask_reply.jsp");
	}
	
	public void saveReply() {
		String askId = getPara("askId");
		String text = getPara("text");
		String userId = super.getCurrentUserId();
		try {
			AskReply.dao.replyAsk(askId, userId, text);
			renderJson(success("回复成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("回复失败！！"));
		}
	}
	
	public void list() {
		int pageNo = getParaToInt("pageNo", 1);
		Page<Ask> askPage = Ask.dao.findAsksByPage(pageNo, super.getCurrentUserType(), super.getCurrentUserId());
		setAttr("askPage", askPage);
		renderJsp("ask_table.jsp");
	}
	
	public void del() {
		try {
			Ask.dao.deleteById(getPara());
			renderJson(success("删除成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("删除失败！！"));
		}
	}

}
